import { ElMessage } from "element-plus";
import { defineStore } from "pinia";
import { ref } from "vue";
import {getSYSDictAPI} from "@/network/sys-dict";
import {SYS_DICT} from "@/dict/dict-sys";



export const useSysDictStore = defineStore("sysDict", () => {
    const sysDict = ref([])
    const PORT = ref({})
    const ABNORMAL_TYPE = ref({})
    const PAY_TYPE = ref({})
    const STUDENT_STATUS = ref({})
    const SOURCE = ref({})
    const COURSE_TYPE = ref({})
    const INTERVIEW_RESULT = ref({})
    const SCHOOL_TYPE = ref({})
    function getSysDict(idList) {
        getSYSDictAPI().then(res=>{
            if(!res){
                return ElMessage.error("未收到返回")
            }

            if(res.status !== 200){
                return ElMessage.error(res.message?res.message:res.status)
            }

            if(!res.data){
                return ElMessage.error("返回的数据为空")
            }

            if(res.data.code !== 0) {
                return ElMessage.error(res.data.message)
            }

            if(res.data.code===0 && res.data.data){
                sysDict.value = res.data.data
                if(idList){
                    idList.forEach(id=>{
                        sysDict.value.forEach(item=>{
                            if(item["sys_dict_id"] === id){
                                const details = item["sys_dict_details"];
                                //此处切记要用this调用，否则会出现this指向性问题，this会指向undefined
                                this.setDictDetail(id, details)
                            }
                        });
                    })
                }else{
                    sysDict.value.forEach(item=>{
                            const id = item["sys_dict_id"]
                            const details = item["sys_dict_details"];
                            //此处切记要用this调用，否则会出现this指向性问题，this会指向undefined
                            this.setDictDetail(id, details)
                    });
                }
            }
        }).catch(error=>{
            ElMessage.error(error.message)
        })
    }

    function setDictDetail(id, details){
        switch (id){
            case SYS_DICT.PORT:
                details.forEach(item=>this.PORT[item["sys_dict_item_id"]] = item["sys_dict_item_name"]);
                break;
            case SYS_DICT.ABNORMAL_TYPE:
                details.forEach(item=>this.ABNORMAL_TYPE[item["sys_dict_item_id"]] = item["sys_dict_item_name"]);
                break;
            case SYS_DICT.PAY_TYPE:
                details.forEach(item=>this.PAY_TYPE[item["sys_dict_item_id"]] = item["sys_dict_item_name"])
                break;
            case SYS_DICT.STUDENT_STATUS:
                details.forEach(item=>this.STUDENT_STATUS[item["sys_dict_item_id"]] = item["sys_dict_item_name"])
                break;
            case SYS_DICT.SOURCE:
                details.forEach(item=>this.SOURCE[item["sys_dict_item_id"]] = item["sys_dict_item_name"])
                break;
            case SYS_DICT.COURSE_TYPE:
                details.forEach(item=>this.COURSE_TYPE[item["sys_dict_item_id"]] = item["sys_dict_item_name"])
                break;
            case SYS_DICT.INTERVIEW_RESULT:
                details.forEach(item=>this.INTERVIEW_RESULT[item["sys_dict_item_id"]] = item["sys_dict_item_name"])
                break;
            case SYS_DICT.SCHOOL_TYPE:
                details.forEach(item=>this.SCHOOL_TYPE[item["sys_dict_item_id"]] = item["sys_dict_item_name"])
                break;
            default:
                break;
        }
    }
    return {
        sysDict,
        PORT,
        ABNORMAL_TYPE,
        PAY_TYPE,
        STUDENT_STATUS,
        SOURCE,
        COURSE_TYPE,
        INTERVIEW_RESULT,
        SCHOOL_TYPE,
        getSysDict,
        setDictDetail,
    }
})

